const db = require('../config/db').sequelize()
const User = db.import('../schema/user.js')
const crypto = require('crypto')
User.sync({force: false});

class UserModel{
  /**
   * 创建用户模型
   * @param data
   * @return {Promise.<*>}
   */
  static async createUser(data){
    return await User.create({
      nickname: data.nickname,
      password: crypto.createHash('md5').update(data.password).digest('hex'),
      avator: data.avator,
      gender: data.gender,
      desc: data.desc,
      email: data.email
    })
  }

  /**
   * find User Model
   * @param email
   * @return {Promise.<*>}
   */
  static async findUser(email){
    return await User.findOne({
      where: {
        email
      }
    })
  }

  /**
   * change userinfo
   * @param {Object} info 
   * @param {*} email
   * @returns {Promise.<*>} 
   */
  static async updateUser(info,email){
    return await User.update({
      info
    },{
      where: {
        email
      }
    })
  }
}

module.exports = UserModel